package com.c3po.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class ShardedRedisUtil {
	public final static String VIRTUAL_COURSE_PREX = "_lc_vc_";



	/**
	 * 切片链接池
	 */
	private ShardedJedisPool shardedJedisPool;
	private String ip="127.0.0.1";
//	private String port=null;

	
	/**
	 * 构造函数
	 */
	public ShardedRedisUtil() {
	}

	/**
	 * 初始化切片池
	 */
	private void initialShardedPool() {
		// 池基本配置
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(100);
		config.setMaxIdle(5);
		config.setMaxWaitMillis(1000);
		config.setTestOnBorrow(true);
		// slave链接
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		shards.add(new JedisShardInfo(ip, 6379));
		// shards.add(new JedisShardInfo(ip, 6379));
		// 构造池
		shardedJedisPool = new ShardedJedisPool(config, shards);
	}

	/**
	 * 在多线程环境同步初始化
	 */
	private synchronized void shardedPoollInit() {
		if (shardedJedisPool == null) {
			initialShardedPool();
		}
	}

	/**
	 * 切片客户端链接 同步获取切片ShardedJedis实例
	 * 
	 * @return ShardedJedis
	 */
	public synchronized ShardedJedis getShardedJedis() {
		if (shardedJedisPool == null) {
			shardedPoollInit();
		}
		ShardedJedis shardedJedis = null;
		try {
			if (shardedJedisPool != null) {
				shardedJedis = shardedJedisPool.getResource();
				// shardedJedis.auth(redisCacheConfig.getAuth());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			returnResource(shardedJedis);
		}
		return shardedJedis;
	}


	/**
	 * 释放ShardedJedis资源
	 * 
	 * @param jedis
	 */
	@SuppressWarnings("deprecation")
	public void returnResource(final ShardedJedis shardedJedis) {
		if (shardedJedis != null && shardedJedisPool != null) {
			shardedJedisPool.returnResource(shardedJedis);
		}
	}

	/**
	 * 得到Key
	 * 
	 * @param key
	 * @return
	 */
	public String buildKey(String key) {
		return VIRTUAL_COURSE_PREX + key;
	}

	/**
	 * 设置 String
	 * 
	 * @param key
	 * @param value
	 */
	// public void setString(String key ,String value){
	// try {
	// value = StringUtil.isNullOrEmpty(value) ? "" : value;
	// getJedis().set(buildKey(key),value);
	// } catch (Exception e) {
	//
	// }
	// }

	/**
	 * 设置 过期时间
	 * 
	 * @param key
	 * @param seconds
	 *            以秒为单位
	 * @param value
	 */
	// public void setString(String key ,int seconds,String value){
	// try {
	// value = StringUtil.isNullOrEmpty(value) ? "" : value;
	// getJedis().setex(buildKey(key), seconds, value);
	// } catch (Exception e) {
	//
	// }
	// }

	public boolean exist(String key) {
		String bKey = buildKey(key);
		if (getShardedJedis() == null || !getShardedJedis().exists(bKey)) {
			return false;
		}
		return true;
	}

	/**
	 * 向缓存中设置对象
	 * 
	 * @param key
	 * @param bean
	 */
	public <T> void setString(String key, String param) {
		String bKey = buildKey(key);
		try {
			getShardedJedis().set(bKey.getBytes(), SerializeUtil.serialize(param));
		} catch (Exception e) {

		}
	}

	/**
	 * 获取String值
	 * 
	 * @param key
	 * @return value
	 */
	public String getString(String key) {
		String bKey = buildKey(key);
		if (getShardedJedis() == null || !getShardedJedis().exists(bKey)) {
			return null;
		}
		byte[] in = getShardedJedis().get(bKey.getBytes());
		String str = (String)SerializeUtil.unserialize(in);
		return str;
	}

	/**
	 * 向缓存中设置对象
	 * 
	 * @param key
	 * @param bean
	 */
	public <T> void setBean(String key, Object bean) {
		String bKey = buildKey(key);
		try {
			getShardedJedis().set(bKey.getBytes(), SerializeUtil.serialize(bean));
		} catch (Exception e) {

		}
	}

	/**
	 * 根据key 获取对象
	 * 
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getBean(String key) {
		String bKey = buildKey(key);
		if (getShardedJedis() == null || !getShardedJedis().exists(bKey.getBytes())) {
			return null;
		}
		byte[] in = getShardedJedis().get(bKey.getBytes());
		T bean = (T) SerializeUtil.unserialize(in);
		return bean;

	}

	/**
	 * 设置 list
	 * 
	 * @param <T>
	 * @param key
	 * @param value
	 */
	public <T> void setList(String key, List<T> list) {
		String bKey = buildKey(key);
		try {
			getShardedJedis().set(bKey.getBytes(), SerializeUtil.serialize(list));
		} catch (Exception e) {

		}
	}

	/**
	 * 获取list
	 * 
	 * @param <T>
	 * @param key
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public <T> List<T> getList(String key) {
		String bKey = buildKey(key);
		if (getShardedJedis() == null || !getShardedJedis().exists(bKey.getBytes())) {
			return null;
		}
		byte[] in = getShardedJedis().get(bKey.getBytes());
		List<T> list = (List<T>) SerializeUtil.unserialize(in);
		return list;
	}

	/**
	 * 设置 map
	 * 
	 * @param <T>
	 * @param key
	 * @param value
	 */
	public <T> void setMap(String key, Map<String, T> map) {
		String bKey = buildKey(key);
		try {
			getShardedJedis().set(bKey.getBytes(), SerializeUtil.serialize(map));
		} catch (Exception e) {

		}
	}

	/**
	 * 获取list
	 * 
	 * @param <T>
	 * @param key
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public <T> Map<String, T> getMap(String key) {
		String bKey = buildKey(key);
		if (getShardedJedis() == null || !getShardedJedis().exists(bKey.getBytes())) {
			return null;
		}
		byte[] in = getShardedJedis().get(bKey.getBytes());
		Map<String, T> map = (Map<String, T>) SerializeUtil.unserialize(in);
		return map;
	}

}
